package org.CrossApp.lib;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.pm.ActivityInfo;
import android.opengl.GLSurfaceView;

public class CrossAppRenderer implements GLSurfaceView.Renderer {

	private final static long NANOSECONDSPERSECOND = 1000000000L;
	private final static long NANOSECONDSPERMICROSECOND = 1000000;
	private static long sAnimationInterval = (long) (1.0 / 60 * NANOSECONDSPERSECOND);
	private long mLastTickInNanoSeconds;
	private CrossAppGLSurfaceView mGLSurfaceView;
	private int mScreenWidth;
	private int mScreenHeight;

	public static void setAnimationInterval(final double pAnimationInterval) {
		sAnimationInterval = (long) (pAnimationInterval * NANOSECONDSPERSECOND);
	}

	public void setScreenWidthAndHeight(final int pSurfaceWidth, final int pSurfaceHeight) {
		this.mScreenWidth = pSurfaceWidth;
		this.mScreenHeight = pSurfaceHeight;
	}

	@Override
	public void onSurfaceCreated(final GL10 pGL10, final EGLConfig pEGLConfig) {
		final int orientation;
		if (((CrossAppActivity) CrossAppActivity.getContext()).getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
			orientation = 1;
		} else if (((CrossAppActivity) CrossAppActivity.getContext()).getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
			orientation = 2;
		} else {
			orientation = 1;
		}
		nativeChangedOrientation(orientation);
		this.crossAppRendererNativeInit();
		this.mLastTickInNanoSeconds = System.nanoTime();
	}

	public void crossAppRendererNativeInit() {
		nativeInit(this.mScreenWidth, this.mScreenHeight);
	}

	@Override
	public void onSurfaceChanged(final GL10 pGL10, final int pWidth, final int pHeight) {
	}

	@Override
	public void onDrawFrame(final GL10 gl) {
		nativeRender();
	}

	private static native void nativeTouchesBegin(final int pID, final float pX, final float pY);

	private static native void nativeTouchesEnd(final int pID, final float pX, final float pY);

	private static native void nativeTouchesMove(final int[] pIDs, final float[] pXs, final float[] pYs);

	private static native void nativeTouchesCancel(final int[] pIDs, final float[] pXs, final float[] pYs);

	private static native boolean nativeKeyDown(final int pKeyCode);

	private static native void nativeRender();

	private static native void nativeInit(final int pWidth, final int pHeight);

	public static native void nativeChanged(final int pWidth, final int pHeight);

	public static native void nativeChangedOrientation(final int type);

	private static native void nativeOnPause();

	private static native void nativeOnResume();

	private static native void nativeCloseKeyPad();

	private static native void nativeOpenKeyPad();

	public void handleActionDown(final int pID, final float pX, final float pY) {
		nativeTouchesBegin(pID, pX, pY);
	}

	public void handleActionUp(final int pID, final float pX, final float pY) {
		nativeTouchesEnd(pID, pX, pY);
	}

	public void handleActionCancel(final int[] pIDs, final float[] pXs, final float[] pYs) {
		nativeTouchesCancel(pIDs, pXs, pYs);
	}

	public void handleActionMove(final int[] pIDs, final float[] pXs, final float[] pYs) {
		nativeTouchesMove(pIDs, pXs, pYs);
	}

	public void handleKeyDown(final int pKeyCode) {
		CrossAppActivity.getContext().runOnGLThread(new Runnable() {
			@Override
			public void run() {
				nativeKeyDown(pKeyCode);
			}
		});
	}

	public void handleOnPause() {
		nativeOnPause();
	}

	public void handleCloseKeyPad() {
		nativeCloseKeyPad();
	}

	public void handleOpenKeyPad() {
		nativeOpenKeyPad();
	}

	public void handleOnResume() {
		nativeOnResume();
	}
	
}